package com.mmc.service.impl;

import com.alibaba.fastjson.JSON;
import com.mmc.entity.LoginUser;
import com.mmc.service.TokenService;
import com.mmc.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 作者：李嘉浩
 * 时间：2024-09-13-17:30
 * 功能：
 */
@Service
public class TokenServiceImpl implements TokenService {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public Map<String,Object> createToken(LoginUser loginuser) {
        Map<String,Object> map=new HashMap<>();
        map.put("userId",loginuser.getUserId());
        String uuid = UUID.randomUUID().toString().replace("-", "");
        map.put("uuid",uuid);
        map.put("userName",loginuser.getUser().getUsername());
        String token = JwtUtils.createToken(map);
        redisTemplate.opsForValue().set("access_token"+uuid, JSON.toJSONString(loginuser));
        Map<String,Object> rMap=new HashMap<>();
        rMap.put("token",token);
        return rMap;
    }
}
